export default{   
  gameState:{
    handler(newState){
      //游戏结束
      clearInterval(this.timer)
      switch(newState){
        case 'fail': 
          this.buttonImg = require('../assets/img/'+'fail.png')
          for(let i = 0 ; i < this.rows ; i++){
            for(let j = 0 ; j < this.columns ; j++){
              if(this.item[i][j].ismine){
                this.item[i][j].clicked = true
              }else if(this.item[i][j].flag == true){
                this.item[i][j].mines = 9
                this.item[i][j].clicked = true
              }
            }
          }
          break;
        case 'success':
          this.buttonImg = require('../assets/img/'+'success.png')
          if(this.checkedBox[0] != 4){
            if(this.time < this.record[this.checkedBox[0]-1]){
              this.$set(this.record,this.checkedBox[0]-1,this.time)
              localStorage.setItem("record",`${this.record[0]},${this.record[1]},${this.record[2]}`)
              this.breakRecord = true
            }
          }
          break;
        case 'playing':
          this.timer = setInterval(() => {
            this.time++
            if(this.time>=999){
              clearInterval(this.timer)
            }
          }, 1000);
          document.onkeydown = null
          break;
        case 'waiting':
          this.buttonImg = require('../assets/img/'+'xiaolian.png')
          let iskeydown = false
          let arr = []
          document.onkeydown=event=>{
            let e = event || window.event
            switch(e.keyCode){
              case 88:
                arr[0] = 'x'
                iskeydown = true
                break;
              case 89:
                if(arr[0] == 'x' && !arr[3] && iskeydown){
                  arr[1] = 'y'
                }else if(arr[3] == 'z' && !arr[4] && iskeydown){
                  arr[4] = 'y'
                }else{
                  iskeydown = false
                  arr = []
                }
                break;
              case 90:
                if(arr[1] == 'y' && !arr[2] && iskeydown){
                  arr[2] = 'z'
                }else if(arr[2] == 'z' && !arr[3] && iskeydown){
                  arr[3] = 'z'
                }else{
                  iskeydown = false
                  arr = []
                }
                break;
              case 16:
                if(arr[4]=='y'){
                  this.openCheat = true
                  let div = document.createElement('div')
                  document.documentElement.appendChild(div)
                  div.style.width = '1px'
                  div.style.height = '1px'
                  div.style.backgroundColor = '#000'
                  div.style.position = 'absolute'
                  div.style.right = '0px'
                  div.style.top = '0px'
                  document.onkeydown = null
                }
                break;
              default: 
                iskeydown = false
                arr = []
            }
          }
        }
    },
    immediate:true
  },
  noClick:function(num){
    if(num == this.totalMines){
      for(let i = 0 ; i< this.rows ; i++){
        for(let j = 0 ; j< this.columns ; j++){
          if(this.item[i][j].clicked === false && this.item[i][j].flag === false){
            this.item[i][j].flag = true
            this.mines--
          }
        }
      }
      this.gameState = 'success'
    }
  }
}